﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Travel
{
    public class Travel:ITravel
    {
        public void InsertTravel(int userId, int travelDistance, Model.TravelType type, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            Model.Travel t = new Model.Travel();
            t.TravelType = type;
            t.DateTimeStamp = datetime;
            t.UserID = userId;
            context.Travels.AddObject(t);
            context.SaveChanges();

        }

        public void InsertTravel(Model.Travel travel, ref Model.NutriLeafDBEntities context)
        {
            context.Travels.AddObject(travel);
            context.SaveChanges();
        }

        public int GetTravelDistanceForADay(int userId, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            return(int) context.Travels.Where(p => p.UserID == userId && p.DateTimeStamp.CompareTo(datetime) == 0).First().TravelDistance;
        }

        public int GetTravelDistanceForADayByType(int userId, DateTime datetime, Model.TravelType type, ref Model.NutriLeafDBEntities context)
        {
             return(int) context.Travels.Where(p => p.UserID == userId && p.DateTimeStamp.CompareTo(datetime) == 0 && p.TravelType==type).First().TravelDistance;
        
            
          
        }

        public Model.Travel GetRecentTravel(int userId, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public Model.Travel GetRecentTravelForADay(int userId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public Model.Travel GetTravel(int travelId, ref Model.NutriLeafDBEntities context)
        {
            return context.Travels.Where(p => p.TravelID == travelId).First();
        }

        public List<Model.Travel> GetTravelForADay(int userId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {
            return context.Travels.Where(p => p.UserID == userId && p.DateTimeStamp.CompareTo(dateTime)==0).ToList();
        }

        public List<Model.Travel> GetTravel(int userId, DateTime dateTimestart, DateTime datetimeEnd, ref Model.NutriLeafDBEntities context)
        {
          return context.Travels.Where(p => p.UserID == userId && p.DateTimeStamp >=dateTimestart && p.DateTimeStamp<datetimeEnd).ToList();
        
        }

        public List<Model.Travel> GetTravel(int userId, int top, ref Model.NutriLeafDBEntities context)
        {
            return context.Travels.Where(p => p.UserID == userId).Take(top).ToList();
        }

        public void RemoveTravel(int travelId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.Travels.Where(p => p.TravelID == travelId).First());
            context.SaveChanges();
        }

        public void UpdateTravel(Model.Travel travel, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }
    }
}